package logic

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"house/model/mysql"

	"house/house"
	"house/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderAddLogic {
	return &OrderAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderAddLogic) OrderAdd(in *house.OrderAddRequest) (*house.OrderAddResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var u mysql.User
	u.ID = uint(in.UserId)
	if err = u.Find(); err != nil {
		logx.Error("未查询到此用户")
		return nil, errors.New("未查询到此用户")
	}
	var s mysql.Shop
	s.ID = uint(in.HouseId)
	if err = s.Find(); err != nil {
		logx.Error("未查询到此商品")
		return nil, errors.New("未查询到此商品")
	}
	var r mysql.Reservation
	r.ReservationSn = uuid.NewString()
	r.UserId = int(in.UserId)
	if err = r.Create(); err != nil {
		logx.Error("生成失败")
		return nil, errors.New("生成失败")
	}
	var a mysql.AppointmentDetails
	a.HouseId = int64(s.ID)
	a.Title = s.Title
	a.Num = in.Num
	a.Image = s.Image
	a.Price = s.Price
	if err = a.Create(); err != nil {
		logx.Error("生成失败1")
		return nil, errors.New("生成失败1")
	}

	return &house.OrderAddResponse{
		Url: r.ReservationSn,
	}, nil
}
